<template>
    <el-dialog :title="!dataForm.id ? '新建人员与工厂绑定关系' : '编辑人员与工厂绑定关系'" :close-on-click-modal="false" :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll  width="600px" >
        <el-row :gutter="15" class="">
            <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules" >
                <el-col :span="24">
                    <el-form-item label="人员" prop="userIds">
                        <user-select v-model="dataForm.userIds" placeholder="选择人员" multiple @change="onChange" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="所属地点" prop="site">
                        <SHZY-select :list="siteList" placeholder="请选择"
							v-model:bindValue="dataForm.site" isConcat label="fullName" value="enCode"></SHZY-select>
					</el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="有效标志" prop="enabledMark">
                        <el-switch v-model="dataForm.enabledMark" :active-value="1" :inactive-value="0" >
                        </el-switch>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading" >确 定</el-button
            >
        </span>
    </el-dialog>
</template>
<script>
import { GetInfo, Create, Update} from '@/api/common/usersiteRel';
import siteApi from "@/api/common/factorystructure";
export default {
    components: {},
    props: [],
    data() {
        return {
            btnLoading: false,
            loading: false,
            visible: false,
            dataForm: {
                id: "",
                userIds: [],
                site: undefined,
                enabledMark: 1,
                toUserName: ''
            },
            rules: {
                site: [{ required: true, message: '请选择所属地点', trigger: '"blur"' }]
            },
            siteList: [],
        };
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {},
    methods: {
        goBack() {
            this.$emit("refresh");
        },
        init(id) {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["elForm"].resetFields();
                if (this.dataForm.id) {
                    GetInfo(this.dataForm.id).then(res => {
                        this.dataForm = res.data;
                    });
                }
                siteApi.getFactoryList().then(res => {
					this.siteList = res.data.list
				})
            });
            //this.$store.commit("generator/UPDATE_RELATION_DATA", {});
        },
        dataFormSubmit() {
            this.$refs["elForm"].validate(valid => {
                if (valid) {
                    this.btnLoading = true;
                    const formMethod = !this.dataForm.id ? Create : Update;
                    formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
                }
            });
        },
        onChange(id, selectedData) {
            if (!id) return this.dataForm.toUserName = ''
            this.dataForm.toUserName = selectedData.fullName
        }
    }
};
</script>
